<?php include_once($_SERVER['DOCUMENT_ROOT']."/controller/common/common_setting_unit.php");         // 引入通用的设置文件 ?>
<?php include_once($_SERVER['DOCUMENT_ROOT']."/controller/common/module/head.php"); // 引入通用的头部 ?>
<?php include_once("../controller/dashboard.php");   ?>

<style>
    #content li{margin: 15px 0}
</style>


<body>

<!--顶部栏-->
<div id="topbar"> <?php include $_SERVER['DOCUMENT_ROOT'] . '/controller/common/module/topbar.php'; ?></div>

<!--容器-->
<div id="container" class="<? echo $now_extender ?>">

    <!--侧边栏-->
    <div id="sidebar"><?php include $_SERVER['DOCUMENT_ROOT'] . '/controller/common/module/sidebar.php'; ?></div>





    <?
    //==============================================================================
    //这里开始才是平常开发需要注重的部分，其余的基本不用理会
    //==============================================================================
    ?>


        <div id="content">

            <?php $page_title='统计面板';?>
            <?php include ROOT_PATH_ . '/controller/common/module/mod-page-title.php'; ?>



<ul>
    <? $order_List_full=select_lines_all_items($table_prefix.'order_info',"","pay_status=1 or pay_status = 3","")?>
    <? $_order_List=transform_2darray_in_1darray_number_as_key($order_List_full,'pay_status');

    $condition['data']=$_order_List;
    $condition['target']='1';

    ?>
    <li><h1>已支付订单数量（不含待退款）</h1> <? echo count_num_in_array($condition);?></li>


    <li><h1>已支付订单数量（含待退款）</h1> <? echo  count($_order_List);?></li>




    <li><h1>应该有配送数据（只计算已支付的订单）</h1>
        <? //查找所有产品，看一个产品有多少数量
           $product_set_list=select_lines_all_items($table_prefix.'product_data',"","1 = 1 ","");
           //提取产品nid=》数量的数组
           $product_nid2amount_list=transform_2darray_in_1darray_with_new_key($product_set_list,'nid','amount');
           //print_r2($product_nid2amount_list);
           ?>

        <?//循环所有订单，看看对应套餐应该有几多条数据

        $number_of_order_detail=0;

        foreach ($order_List as $index => $order)
        {
            $number_of_order_detail=$number_of_order_detail+$product_nid2amount_list[$order['product_id']];
            $number[]=$product_nid2amount_list[$order['product_id']];
            //echo "这次：".$order['product_id']." > ".$product_nid2amount_list[$order['product_id']];
            //echo "<br>";
        }
        //echo $number_of_order_detail;
        echo array_sum($number);
        ?>

    </li>


    <? $order_List_all=select_lines_all_items($table_prefix.'order_info',"","pay_status=1 or pay_status = 3","")?>

    <li><h1>应该有配送数据（含待退款）</h1>
        <? //查找所有产品，看一个产品有多少数量
        $product_set_list=select_lines_all_items($table_prefix.'product_data',"","1 = 1 ","");
        //提取产品nid=》数量的数组
        $product_nid2amount_list=transform_2darray_in_1darray_with_new_key($product_set_list,'nid','amount');
        //print_r2($product_nid2amount_list);
        ?>

        <?//循环所有订单，看看对应套餐应该有几多条数据

        $number_of_order_detail2=0;

        foreach ($order_List_all as $index => $order)
        {
            $number_of_order_detail=$number_of_order_detail+$product_nid2amount_list[$order['product_id']];
            $number2[]=$product_nid2amount_list[$order['product_id']];

        }
        echo array_sum($number2);
        ?>

    </li>




    <li>
        <h1>实际有订单数据</h1>
        <?
        ini_set('memory_limit', '256M');
        $order_detail_List=select_lines_items($table_prefix.'order_detail',"","nid,order_nid",
            "status= '正常' ","");
        ?>

        <?echo count($order_detail_List);?>
    </li>




    <li>
        <h1>比较订单数据和配送表数据</h1>
        <?
        $order_nid_of_order_detail_list=transform_2darray_in_1darray_number_as_key($order_detail_List,'order_nid');
        //去重
        $order_nid_of_order_detail_list=array_unique($order_nid_of_order_detail_list);


        $order_nid_of_order_info_list=transform_2darray_in_1darray_number_as_key($order_List,'order_id');

        //比较两个数组，把没有重复的提取出来
        $order_nid_of_order_detail_list_not_in_order_info_list=array_diff($order_nid_of_order_detail_list,$order_nid_of_order_info_list);

        ?>

        <?echo count($order_detail_List);?>
    </li>





    <?
    $sqlstr="
SELECT *,
            COUNT(*) AS duplicate_count,
            GROUP_CONCAT(order_id) AS related_ids,
            GROUP_CONCAT(DISTINCT order_name) AS all_order_names  -- 关键：拼接所有order_name
          FROM athlon_order_info 
          WHERE pay_status = 1 
          GROUP BY student_name, student_school, student_grade, student_class 
          HAVING duplicate_count > 1
          ";
    $result = do_query($sqlstr);
    // 循环获取所有记录
    while ($row = mysqli_fetch_array($result)) {
        $duplicate_order_List[] = $row;
    }


?>

    <li><h1>重复的订单</h1></li>

    <?
    foreach ($duplicate_order_List as $index => $order){?>
    <li>
        <?echo $order['order_id'];?> - <?echo $order['order_sn'];?>  -
        <?echo $order['student_name'];?> - <?echo $order['student_school'];?> - <?echo $order['student_grade'];?> - <?echo $order['student_class'];?> -
        【<?echo $order['all_order_names'];?> 】-
        【<?echo $order['duplicate_count'];?>】
    </li>

    <?;}?>




    <? $refund_order_List = select_lines_all_items($table_prefix . 'order_info', "",
        "pay_status=2", "");
       $refund_order_List_id_list = transform_2darray_in_1darray_number_as_key($refund_order_List, 'order_id');
       $refund_order_List_id_str=implode("','", $refund_order_List_id_list);

       //查看配送日程是否还有存在配送
       $schedule_List = select_lines_all_items($table_prefix . 'order_detail', "",
        "order_nid in ('".$refund_order_List_id_str."') and status = '正常' ", "");

    ?>



    <li><h1>已退款但是继续配送</h1>
        <? echo count($schedule_List);?></li>






    <li><h1>配送数据库异常的</h1></li>
    <li>
        <? $order_detail_error_List=select_lines_items($table_prefix.'order_detail',"","nid,name",
            "status = '' OR status IS NULL OR
        school = '' OR school IS NULL OR
        grade = '' OR grade IS NULL OR
        class_ = '' OR class_ IS NULL OR
        order_nid = '' OR order_nid IS NULL OR
        product_nid = '' OR product_nid IS NULL OR
        student_nid = '' OR student_nid IS NULL OR
        user_nid = '' OR user_nid IS NULL OR
        name = '' OR name IS NULL","");

        print_r2($order_detail_error_List);

        ?>


    </li>



    <li><h1>同一订单号的产品数量</h1></li>
    <li>
        <?
        $sql="SELECT order_nid,
       COUNT(*) AS qty
FROM athlon_order_detail where status = '正常'
GROUP BY order_nid;";

        $result2 = do_query($sql);
        while ($row = mysqli_fetch_array($result2))
        {
            $values2[] = $row;
        }
        ?>


        <?
        foreach ($values2 as $index2 => $value2)
        {
            //echo $value2['order_nid']." - ".$value2['qty']."<br>";
        }
        ?>


<!--        //帮我统计这个数组，$values2，里面不同的qty值，的数量-->
        <?
        $qty_list=transform_2darray_in_1darray_number_as_key($values2,'qty');
        //一维数组中，值相同的数量
        $qty_list_count=array_count_values($qty_list);
        print_r2($qty_list_count);


        ?>
    </li>




    <li><h1>每个日期配送的产品数量</h1></li>
    <li>
        <?
        $sql="SELECT ondate,
       COUNT(*) AS row_count
FROM athlon_order_detail where status = '正常'
GROUP BY ondate
ORDER BY ondate";

        $result = do_query($sql);
        while ($row = mysqli_fetch_array($result))
        {
            $values[] = $row;
        }
        ?>


        <?
        foreach ($values as $index => $value)
        {
            echo $value['ondate']." - ".$value['row_count']."<br>";
        }
        ?>
    </li>

</ul>




<!--container端點01-->
</div>



    <!--底部栏-->
    <?php include ROOT_PATH_ . "/controller/common/module/footer.php"; ?>

</body>
</html>
